package personal.xjl.plusdemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import personal.xjl.plusdemo.domain.Student;
import personal.xjl.plusdemo.service.StudentService;

import java.util.List;

@Controller
@RequestMapping("Student")
public class StudentController {
    private static final Log log= LogFactory.getLog(StudentController.class);
    @Autowired
    StudentService studentService;
    @RequestMapping("list.do")
    public String list(Model model,@RequestParam(value = "currPage",defaultValue = "1")int currPage,@RequestParam(value = "size",defaultValue = "3")int size) {
        log.warn("用户打开了学生信息表");
       /* List<Student> studentList = studentService.list();
        model.addAttribute("studentList", studentList);*/
        Page<Student> page = studentService.page(new Page<>(currPage,size));
        model.addAttribute("page", page);

        return "listStudents";
    }
    @RequestMapping("delete.do")
    //  @ResponseBody
    public String delete(@RequestParam("id") int id,Model model) {
        studentService.removeById(id);
        return "redirect:/Student/list.do";
    }
    @RequestMapping("page.do")
    public String page(@RequestParam("page") String page) {
        return page;
    }
    @RequestMapping("add.do")
    public String add(Student student) {
        System.out.println(student);
        studentService.save(student);
        return "redirect:/Student/list.do";
    }
    //准备修改学生信息
    @RequestMapping("toEdit.do")
    public String edit(@RequestParam("id") int id, Model model) {
        Student oldStudent=studentService.getById(id);
        model.addAttribute("student",oldStudent);
        return "editStudent";
    }
    //修改学生信息
    @RequestMapping("edit.do")
    public String edit(Student student) {
        studentService.saveOrUpdate(student);
        return "redirect:/Student/list.do";
    }
    /*    //登录,如果登录成功，跳转到list.do，显示学生数据；如果登录失败，回到login页面
        @RequestMapping("login.do")
        public String login(HttpSession session, @RequestParam("username") String username, @RequestParam("password") String password) {
            if(studentService.login(username,password)) {
                //发放登录的令牌，username
                session.setAttribute("username",username);
                return "redirect:/Student/list.do";
            }
            else
                return "login";
        }*/
/*    @RequestMapping("loginNew.do")
    public String loginNew() {
        return "login";
    }*/
}
